<?php

require_once dirname(__FILE__) . '/../lib/sfGuardUserGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/sfGuardUserGeneratorHelper.class.php';

/**
 * sfGuardUser actions.
 *
 * @package    sfGuardPlugin
 * @subpackage sfGuardUser
 * @author     Fabien Potencier
 * @version    SVN: $Id: actions.class.php 23319 2009-10-25 12:22:23Z Kris.Wallsmith $
 */
class sfGuardUserActions extends autoSfGuardUserActions {

    public function executeActivateBySms(sfWebRequest $request) {
        $id = $this->getRequestParameter('id');
        $this->sendActivateSMS($id);

        $this->getUser()->setFlash('notice', I18nUtilities::__('SMS with new password was sent successfully'));
        $this->redirect('sfGuardUser/index');
    }

    public function executeBatchActivateBySms(sfWebRequest $request) {
        $ids = $this->getRequestParameter('ids');

//TODO: implement..
        foreach ($ids as $id) {
            $this->sendActivateSMS($id);
        }
        $this->getUser()->setFlash('notice', I18nUtilities::__('SMS with new password was sent successfully'));
        $this->redirect('sfGuardUser/index');
    }

    protected function sendActivateSMS($id) {
        $user = sfGuardUserTable::getInstance()->find($id);
        $teacher = TeacherTable::getInstance()->retrieveByIntgId($user->getUsername());
        $rand_pass = $user->setRandomPassword();
        $user->setIsActive(true)->save();
        if (is_object($teacher)) {
            if ($teacher->getIsManager()) {
                $message = I18nUtilities::__('MANAGER_ACTIVATE_SMS_MSG') . chr(10);
                $message = str_replace('%SCHOOL_NAME%', $teacher->getSchool(), $message);
            } elseif ($teacher->getIsTrainer()) {
                $message = I18nUtilities::__('TRAINER_ACTIVATE_SMS_MSG') . chr(10);
            }
        } else {
            $message = I18nUtilities::__('SYSTEM_USER_ACTIVATE_SMS_MSG') . chr(10);
        }
        $message .= I18nUtilities::__('Username') . ' : ' . $user->getUsername() . chr(10);
        $message .= I18nUtilities::__('Password') . ' : ' . $rand_pass . ' ' . chr(10);
        $message .= I18nUtilities::__('Url') . ' : ' . sfConfig::get('app_static_url');

        $phone_number = sfConfig::get('app_mobile_prefix', '00966') . $user['phone'];
        globals::send_sms($message, $phone_number);
    }

    public function executeExport(sfWebRequest $request) {
        $q = parent::buildQuery();

        $q->select('
            username
            , first_name
            , last_name
            , is_active
            , last_login
            , school.name as school_name
            , groups.description as groups
            , teacher.id
            '
        );

        $q->leftJoin('r.Teacher as teacher');
        $q->leftJoin('teacher.School as school');
        $q->leftJoin('r.Groups as groups');

        $items = $q->fetchArray();

        $order = array(
            'username'
            , 'first_name'
            , 'last_name'
            , 'is_active'
            , 'last_login'
            , 'school_name'
            , 'groups'
        );

        $labels = array(
            'username' => 'Username'
            , 'is_active' => 'Is active'
            , 'first_name' => 'First name'
            , 'last_name' => 'Last name'
            , 'last_login' => 'Last login'
            , 'school_name' => 'School'
            , 'groups' => 'Groups'
        );

        $partial = $this->getPartial('teacher/export', array('items' => $items, 'order' => $order, 'labels' => $labels));

        $word = globals::wordify($partial);

        header('Content-type: application/doc');
        header('Content-Disposition: attachment; filename="users_list_' . (substr(time(), 0, 5)) . '.doc"');
        header('Content-Description: File Transfer');

        die($word);
    }

    public function executeSendCustomSms(sfWebRequest $request) {
        $this->setLayout('popupLayout');

        $this->send_succ = false;

        $q = $this->buildQuery();
        $this->user_count = $q->count();
        if (!$this->user_count)
            return;

        $this->form = new sendSmsForm();


        if ($request->isMethod('POST')) {
            $data = $this->getRequestParameter($this->form->getName());
            $this->form->bind($data);

            if ($this->form->isValid()) {
                $values = $this->form->getValues();

                $q->select('id')->addSelect('phone');
                $phone_nums = array();

                foreach ($q->fetchArray() as $row) {
                    $phone_nums[] = $row['phone'];
                }

                globals::send_sms($values['sms_body'], $phone_nums);

                $this->send_succ = true;
            }
        }
    }

}